<?php


namespace app\api\controller;


use think\facade\Db;

class AuthRule
{
    //获取列表的数据
    public function getAuthRule(){
        $param_data = request()->param();
        unset($param_data['page']);
        //查询所有菜单
        $data = Db::table('think_auth_rule')->where($param_data)->select()->toArray();
        $datas =  $this->getTree($data);
        //var_dump($datas);die();
//        if(count($data)>1){//如果有多个
//            $datas = $this->getTree($data);//形成树形
//            $city = $datas;
//        }else{
//            $city = $data;
//            $datas = $data;
//        }
        return json(['status'=>200,'data'=>$datas,'total'=>count($datas)]);
    }
//    public function getCity2(){
//        $city = $this->getTree2();
//        return json(['status'=>200,'data'=>$city,'total'=>count($city)]);
//    }
    //递归
    protected function getTree($data,$f_id =0)
    {
        $list = [];
        foreach ($data as $value) {
            //第一次遍历，找到父节点为0的
            if ($value['f_id'] == $f_id) {
                //开始递归，查找父id为该节点id的节点
                if (!empty($this->getTree($data, $value['id']))) {
                    $value['children'] = $this->getTree($data, $value['id']);
                }
                $list[] = $value;
            }
        }
        return $list;
    }
    public function getMenus(){
        return json(['status'=>200,'data'=>$this->getTree2()]);
    }
    protected function getTree2($f_id=0,$level=1){
        $zhi =  Db::table('think_auth_rule')->select()->toArray();
        static $arr =array();
        foreach($zhi as $key=>$value){
            if($value['f_id']==$f_id){
                if($level<3){
                    $value['level']=$level;
                    $arr[] =$value;
                    //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                    $this->getTree2($value['id'],$level+1);
                }
            }
        }
        return $arr;
    }
    //新增权限
    public function createAuthRule(){
        $data = request()->param();
        if($data['l_type'] == 0){
            $data['level'] = 1;
            $data['f_id'] = 0;
            $data['l_component'] = 'Layout';
        }else{
            $data['level'] = Db::table("think_auth_rule")->where('id',$data['f_id'])->value('level')+1;
        }
        $data['type'] = 1;
        $add = Db::table("think_auth_rule")->insert($data);
        if($add){
            return json(['status'=>200,'msg'=>'添加成功']);
        }else{
            return json(['status'=>500,'msg'=>'添加失败']);
        }
    }
    //修改权限
    public function updateAuthRule(){
        $data = request()->param();
        if($data['id']==$data['f_id']){
            return json(['status'=>500,'msg'=>'自己不能当自己的上级！']);
        }
        $data['level'] = Db::table("think_auth_rule")->where('id',$data['f_id'])->value('level')+1;
        unset($data['children']);
        $data['type'] = 1;
        $update = Db::table("think_auth_rule")->where('id',$data['id'])->update($data);
        if($update){
            return json(['status'=>200,'msg'=>'修改成功']);
        }else{
            return json(['status'=>500,'msg'=>'修改失败']);
        }
    }
    //删除权限
    public function deleteAuthRule(){
        $data = request()->param();
        $sonid =  $this->getSonId($data['id']);
        if(empty($sonid)){//没有子id，删除自己
            $del = Db::table("think_auth_rule")->where('id',$data['id'])->delete();
        }else{
            array_unshift($sonid,$data['id']);
            $del = Db::table("think_auth_rule")->whereIn('id',$sonid)->delete();
        }
        if($del){
            return json(['status'=>200,'msg'=>'删除成功']);
        }else{
            return json(['status'=>500,'msg'=>'删除失败']);
        }
    }
    protected function getSonId($id){
        $data =  Db::table('think_auth_rule')->select()->toArray();
        $arr = [];
        foreach ($data as $val){
            if($val['f_id']==$id){
                $arr[] = $val['id'];
            }
        }
        return $arr;
    }
    //选择上一级权限，
    public function getFatherData(){
        $data = Db::table('think_auth_rule')->select()->toArray();
        $keys = Db::table('think_auth_rule')->where('f_id',0)->column('id');
        return json(['status'=>200,'data'=>$this->getTree3($data),'keys'=>$keys]);
    }
    //递归
    protected function getTree3($data,$f_id =0)
    {
        $list = [];
        foreach ($data as $value) {
            //第一次遍历，找到父节点为0的
            if ($value['f_id'] == $f_id) {
                //开始递归，查找父id为该节点id的节点
                if($value['level']<3) {
                    if (!empty($this->getTree3($data, $value['id']))) {
                        $value['children'] = $this->getTree3($data, $value['id']);
                    }
                    $list[] = $value;
                }
            }
        }
        return $list;
    }

}